#region

using System;
using Gurock.SmartInspect;
using Microsoft.Practices.Unity.InterceptionExtension;

#endregion

namespace SmartInspect.Extensions.Unity
{
    public class SiWrappedCallHandler : SiBaseHandler
    {
        private Session _session;

        public SiWrappedCallHandler()
        {
            Order = 3;
        }

        public override IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            _session = input.Target.GetLogger();
            StartCallHandlerInvoke(input, _session);
            Enter(input, _session);
            IMethodReturn ret = getNext().Invoke(input, getNext);
            Leave(input, _session, ret);
            StopCallHandlerInvoke(input, _session);
            return ret;
        }

        protected virtual void Enter(IMethodInvocation input, Session ses)
        {
            var exc =
                new NotSupportedException(
                    "The class SiWrappedCallHandler and SiWrappedCallAttribute are base classes and may not be used directly and the method Enter() has to be overriden to provide needed functionallity!");
            _session.LogException(exc);
            throw exc;
        }

        protected virtual void Leave(IMethodInvocation input, Session ses, IMethodReturn ret)
        {
            var exc =
                new NotSupportedException(
                    "The class SiWrappedCallHandler and SiWrappedCallAttribute are base classes and may not be used directly and the method Leave() has to be overriden to provide needed functionallity!");
            _session.LogException(exc);
            throw exc;
        }
    }
}